package com.iemes.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class GetFormObject {
    public static <T>T getFormMap(Class<T> clazz, HttpServletRequest request){
        //获取所有请求头信息
        Enumeration<String> headerNames = request.getHeaderNames();
        //循环
        T t = null;

            //根据形参clazz创建对象
        try {
            t = clazz.newInstance();
            //强制转换formMap
            FormMap<String, Object> map = (FormMap<String, Object>) t;
            //循环所有请求信息
            while (headerNames.hasMoreElements()){
                //取出当前数据
                String formName = headerNames.nextElement();
                //根据当前数据取出值
                String formValue = request.getParameter(formName);
                //判断值是否为空
                if(!Common.isEmpty(formValue)){
                    //取出当前类名
                    String clazzName = t.getClass().getSimpleName().toLowerCase();
                    if(formName.toLowerCase().startsWith(clazzName)){
                        //放入数据
                        formName = formName.substring(clazzName.length()+1);
                        map.set(formName, formValue);
                    }
                }
            }
        } catch (InstantiationException e) {
                e.printStackTrace();
        } catch (IllegalAccessException e) {
                e.printStackTrace();
        }


        return null;
    }
}
